package top.llove.utils;

import cn.hutool.setting.Setting;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
import org.eclipse.jgit.transport.CredentialsProvider;
import org.eclipse.jgit.transport.RefSpec;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;

import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;

/**
 * Git操作
 *
 * @author: 幸会
 * @date: 2021/6/8 17:23
 * @email: 1846724873@qq.com
 * @description: Git操作
 * version: 1.0
 */
public class GitUtils {


    private static Setting set = new Setting("config.setting"); // 配置对象

    private static String gitUrl = set.get("xh.img.git"); // Git远程仓库链接
    private static String path = set.get("xh.img.path"); // Git仓库存放目录
    private static File gitDir = new File(path); // 存放目录File对象


    private static CredentialsProvider user = GitUtils.getCredential(set.get("xh.img.user"), set.get("xh.img.pass"));


    /**
     * 凭证管理
     *
     * @param userName git用户名
     * @param password git密码
     * @return
     */
    public static CredentialsProvider getCredential(String userName, String password) {
        return new UsernamePasswordCredentialsProvider(userName, password);
    }






    /**
     * 克隆仓库
     * @return
     * @throws GitAPIException
     */
    public static Git cloneGit() throws GitAPIException {
        return Git.cloneRepository()
                .setURI(gitUrl)
                .setDirectory(gitDir)
                .setCredentialsProvider(user)
                .call();
    }

    /**
     * 提交所有文件到本地仓库
     * @param msg 提交信息
     * @throws IOException
     * @throws GitAPIException
     */
    public static void commit(String msg) throws IOException, GitAPIException {
        Git git = new Git(getRepository()); // 从已有镜像获取Git
        git.add().addFilepattern(".").call(); // 添加所有文件
        git.commit().setMessage(msg).call(); // 提交所有文件到本地仓库
    }

    /**
     * 推送本地仓库到远程服务器
     * @throws IOException
     * @throws GitAPIException
     */
    public static void push() throws IOException, GitAPIException {
        Repository repository = getRepository(); // 从已有目录获取镜像
        Git git = new Git(repository); // 创建Git
        // 推送到远程服务器
        git.push()
                .setCredentialsProvider(user)
                .setRemote("origin")
                .setRefSpecs(new RefSpec(repository.getBranch()))
                .call();
    }

    /**
     * 从远程仓库拉取最近更新
     * @throws IOException
     * @throws GitAPIException
     */
    public static void pull() throws IOException, GitAPIException {
        Repository repository = getRepository(); // 从已有目录获取镜像
        Git git = new Git(repository); // 获取Git
        git.pull()
                .setRemoteBranchName(repository.getBranch())
                .setCredentialsProvider(user)
                .call();
    }

    /**
     * 从已有仓库获取镜像
     * @return 镜像
     * @throws IOException
     */
    public static Repository getRepository() throws IOException {
        return new FileRepositoryBuilder()
                .setGitDir(Paths.get(path, ".git").toFile()).build();
    }




}
